package oocl.ita.eordering.controller;

import java.io.IOException;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.po.ResponsePo;
import oocl.ita.eordering.service.MerchantService;
import oocl.ita.eordering.util.BaseUtil;
import oocl.ita.eordering.util.JSONUtil;
import oocl.ita.eordering.webService.GetMerchantStatusWS;

@Controller
public class IndexController {
	
	@Autowired
	private MerchantService ms;
	
	@Autowired
	JmsTemplate template;
	
	@Autowired(required=true)
	@Qualifier(value="queueDestination")
	Destination dest;
	
	@RequestMapping("/")
	public String index() {
		return "redirect:/linkLogin";
	}
	
	//页面跳转：连接到登录页面
	@RequestMapping("/linkLogin")
	public String linkLogin() {
		return "login";
	}
	
	//页面跳转：连接到首页
	@RequestMapping("/home")
	public String LinkHome(Model model){
		model.addAttribute("mainPage","message.jsp");
		return "home";
	}
	
	//页面跳转：链接到注册页面
	@RequestMapping("/linkRegister")
	public String LinkRegister(){
		return "register";
	}
	
	
	//注销登录
	@RequestMapping("/logout")
	public String logout(HttpServletRequest req){
		HttpSession session = req.getSession();
		session.invalidate();
		return "redirect:/linkLogin";
	}
	
	
	//表单提交：登录，登录成功存session,返回验证结果
	@RequestMapping("/signIn")
	@ResponseBody
	public ResponsePo logint(String tel,String pwd,HttpServletRequest req){
		String psd = BaseUtil.JohnMd(pwd,"md5");
		Merchant m = ms.login(tel, psd);
		
		ResponsePo r = new ResponsePo();
		if (m != null) {
			req.getSession().setAttribute("user", m);
			int state = GetMerchantStatusWS.getMechantPass(m);
			req.getSession().setAttribute("state", state);
			r.setStatus(true);
		} else {
			r.setStatus(false);
		}
		return r;
	}
	
	
	//表单提交：注册，验证，返回结果
	@RequestMapping("/register")
	public String register(String mTel,String mPersonName,String mIdCard,String mLocation,String mBrand,String mPassword, @RequestParam("mCardPath") MultipartFile cardPic,@RequestParam("mLogoPath") MultipartFile logoPic,HttpServletRequest req) throws IllegalStateException, IOException{
		//文件接收，获取相对路径
		String mCardPath=ms.getIdCardPath(mTel, cardPic);
		String mLogoPath=ms.getLogoPath(mTel, logoPic);
		
		//加密
		mPassword=BaseUtil.JohnMd(mPassword,"md5");
		Merchant m=new Merchant(null, mPersonName, mTel, mIdCard, mCardPath, mLocation, mBrand, mLogoPath, mPassword, 10);
		
		int mid = ms.regist(m);
		m.setmId(mid);
		req.getSession().setAttribute("user", m);
		//注册时默认设置为状态1，存入session
		req.getSession().setAttribute("state", 1);
		
		//JMS 发送商户信息
		ita.project4.main.protocol.po.Merchant regMerchant=new ita.project4.main.protocol.po.Merchant(m.getmId(), mPersonName, mTel, mIdCard, mCardPath, mLocation, mBrand);
		String info = JSONUtil.objetc2Json(regMerchant);//转成JSON格式发送
		template.send(dest,new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage msg=session.createTextMessage(info);
				return msg;
			}
		});
		
		return "redirect:/home";
	}
	
	//查询电话号码是否已注册
	@RequestMapping("/checkTel")
	@ResponseBody
	public boolean checkTel(String mTel){
		System.out.println(111111);
		System.out.println(mTel);
		boolean i=ms.isTelExist(mTel);
		return i;
	}

}
